package com.dingreading.cloud.ssxcx.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.ssxcx.dto.CaCallNamesDto;
import com.dingreading.cloud.ssxcx.dto.XcxStudentHomeworkDto;
import com.dingreading.cloud.ssxcx.dto.XcxStudentLearningProgressDto;
import com.dingreading.cloud.ssxcx.dto.XcxStudentQuizAnswersDto;
import com.dingreading.cloud.ssxcx.entity.Cmsubjects;
import com.dingreading.cloud.ssxcx.service.*;
import com.dingreading.cloud.ssxcx.util.DataProcessUtils;
import com.mybatisflex.core.paginate.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class XcxStudentCenterServiceImpl implements XcxStudentCenterService {
    @Resource
    private DataProcessUtils dataProcessUtils;
    @Resource
    private CmsubjectsService cmsubjectsService;
    @Resource
    private CaCallNamesService caCallNamesService;
    @Resource
    private XcxStudentQuizAnswersService xcxStudentQuizAnswersService;
    @Resource
    private XcxStudentHomeworkService xcxStudentHomeworkService;
    @Resource
    private XcxStudentLearningProgressService xcxStudentLearningProgressService;

    @Override
    public R<Object> pageListMistake(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid, String ip) {
        if (StringUtils.isBlank(agencyUid))
            return R.fail("伙伴uid为空！");
        if (StringUtils.isBlank(storeUid))
            return R.fail("门店uid为空！");
        if (StringUtils.isBlank(studentUid))
            return R.fail("学生信息为空！");

        Page<XcxStudentQuizAnswersDto> page = xcxStudentQuizAnswersService.pageListMistake(pageUtil, agencyUid, storeUid, studentUid, projectUid);
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            dataProcessUtils.dealQuizAnswers(page.getRecords(), ip);
        }
        PageDto<XcxStudentQuizAnswersDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> pageListHomework(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid, String ip) {
        if (StringUtils.isBlank(agencyUid))
            return R.fail("伙伴uid为空！");
        if (StringUtils.isBlank(storeUid))
            return R.fail("门店uid为空！");
        if (StringUtils.isBlank(studentUid))
            return R.fail("学生信息为空！");
        Page<XcxStudentHomeworkDto> page = xcxStudentHomeworkService.pageListHomework(pageUtil, agencyUid, storeUid, studentUid, projectUid);
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            dataProcessUtils.dealHomework(page.getRecords(), ip);
        }
        PageDto<XcxStudentHomeworkDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> pageListLearning(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid, String ip) {
        if (StringUtils.isBlank(agencyUid))
            return R.fail("伙伴uid为空！");
        if (StringUtils.isBlank(storeUid))
            return R.fail("门店uid为空！");
        if (StringUtils.isBlank(studentUid))
            return R.fail("学生信息为空！");
        Page<XcxStudentLearningProgressDto> page = xcxStudentLearningProgressService.pageListLearning(pageUtil, agencyUid, storeUid, studentUid, projectUid);
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            dataProcessUtils.dealLearningProgress(page.getRecords(), ip);
        }
        PageDto<XcxStudentLearningProgressDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> pageListSchedule(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid) {
        if (StringUtils.isBlank(agencyUid))
            return R.fail("伙伴uid为空！");
        if (StringUtils.isBlank(storeUid))
            return R.fail("门店uid为空！");
        if (StringUtils.isBlank(studentUid))
            return R.fail("学生信息为空！");

        String uid = null;
        if (StringUtils.isNotBlank(projectUid)){
            Cmsubjects cmsubjects = cmsubjectsService.getBySubjectUid(projectUid);
            if (cmsubjects != null)
                uid = cmsubjects.getUid();
        }
        Page<CaCallNamesDto> page = caCallNamesService.pageListSchedule(pageUtil, agencyUid, storeUid, studentUid, uid);
        PageDto<CaCallNamesDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }
}
